/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.GalMode;
/*     */ import com.zimbra.cs.account.Provisioning.SearchGalResult;
/*     */ import com.zimbra.cs.account.gal.GalConstants;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestGal
/*     */   extends TestCase
/*     */ {
/*  52 */   private String TEST_ID = TestProvisioningUtil.genTestId();
/*  53 */   private static String TEST_CLASS_NAME = "test-gal";
/*  54 */   private static String PASSWORD = "test123";
/*  55 */   private static String ACCT_NAME_PREFIX = "user";
/*  56 */   private static String QUERY = ACCT_NAME_PREFIX;
/*  57 */   private static int NUM_ACCOUNTS = 1000;
/*     */   
/*  59 */   private static int MAX_PAGE_SIZE = 1000;
/*  60 */   private static int UNLIMITED = 0;
/*  61 */   private static int LIMITED = 100;
/*     */   
/*     */ 
/*  64 */   private static int LDAP_SERVER_SIZE_LIMIT = UNLIMITED;
/*  65 */   private String DOMAIN_NAME = TestProvisioningUtil.baseDomainName(TEST_CLASS_NAME, null);
/*  66 */   private String TOKENIZE_TEST_DOMAIN_NAME = TestProvisioningUtil.baseDomainName(TEST_CLASS_NAME + "-tokenize", null);
/*     */   
/*     */   private Provisioning mProv;
/*  69 */   private boolean DEBUG = true;
/*  70 */   private boolean SKIP_ACCT_CHECKING = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static final boolean SOAP_PROV = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static
/*     */   {
/*  84 */     CliUtil.toolSetup();
/*     */   }
/*     */   
/*     */   static enum GalOp {
/*  88 */     GOP_AUTOCOMPLETE, 
/*  89 */     GOP_SEARCH, 
/*  90 */     GOP_SYNC;
/*     */     
/*     */     private GalOp() {}
/*     */   }
/*     */   
/*     */   static abstract interface PageSize
/*     */   {
/*     */     public abstract int pageSize();
/*     */   }
/*     */   
/*     */   static enum PageSizeLdapServerUnlimited implements TestGal.PageSize {
/* 101 */     PS_UNLIMITED(0), 
/* 102 */     PS_MORE_ACCOUNTS(TestGal.more(TestGal.NUM_ACCOUNTS)), 
/* 103 */     PS_LESS_ACOUNTS(TestGal.less(TestGal.NUM_ACCOUNTS));
/*     */     
/*     */     int mPs;
/*     */     
/*     */     private PageSizeLdapServerUnlimited(int ps) {
/* 108 */       if (ps > TestGal.MAX_PAGE_SIZE)
/* 109 */         ps = TestGal.MAX_PAGE_SIZE;
/* 110 */       this.mPs = ps;
/*     */     }
/*     */     
/*     */     public int pageSize() {
/* 114 */       return this.mPs;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   static enum PageSizeLdapServerLimited
/*     */     implements TestGal.PageSize
/*     */   {
/* 122 */     PS_UNLIMITED(0), 
/* 123 */     PS_MORE_LDAP_SERVER_LIMIT(TestGal.more(TestGal.LDAP_SERVER_SIZE_LIMIT)), 
/* 124 */     PS_LESS_LDAP_SERVER_LIMIT(TestGal.less(TestGal.LDAP_SERVER_SIZE_LIMIT));
/*     */     
/*     */     int mPs;
/*     */     
/*     */     private PageSizeLdapServerLimited(int ps) {
/* 129 */       if (ps > TestGal.MAX_PAGE_SIZE)
/* 130 */         ps = TestGal.MAX_PAGE_SIZE;
/* 131 */       this.mPs = ps;
/*     */     }
/*     */     
/*     */     public int pageSize() {
/* 135 */       return this.mPs;
/*     */     }
/*     */   }
/*     */   
/*     */   class PageSizeEnum {
/*     */     int mIdx;
/*     */     TestGal.PageSize[] mEnum;
/*     */     
/*     */     PageSizeEnum() {
/* 144 */       this.mIdx = 0;
/* 145 */       if (TestGal.LDAP_SERVER_SIZE_LIMIT == TestGal.UNLIMITED) {
/* 146 */         this.mEnum = TestGal.PageSizeLdapServerUnlimited.values();
/*     */       } else
/* 148 */         this.mEnum = TestGal.PageSizeLdapServerLimited.values();
/*     */     }
/*     */     
/*     */     public boolean hasNext() {
/* 152 */       return this.mIdx < this.mEnum.length;
/*     */     }
/*     */     
/*     */     public int next() {
/* 156 */       assert (this.mIdx < this.mEnum.length);
/* 157 */       return this.mEnum[(this.mIdx++)].pageSize();
/*     */     }
/*     */   }
/*     */   
/*     */   private String acctName(int index) {
/* 162 */     return ACCT_NAME_PREFIX + "-" + index + "@" + this.DOMAIN_NAME;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void setupDomain(String domainName, String domainBaseDn)
/*     */     throws Exception
/*     */   {
/* 170 */     Domain domain = this.mProv.get(Key.DomainBy.name, domainName);
/* 171 */     if (domain == null) {
/* 172 */       Map<String, Object> attrs = new HashMap();
/*     */       
/* 174 */       attrs.put("zimbraGalLdapURL", "ldap://localhost:389");
/* 175 */       attrs.put("zimbraGalLdapBindDn", LC.zimbra_ldap_userdn.value());
/* 176 */       attrs.put("zimbraGalLdapBindPassword", LC.zimbra_ldap_password.value());
/* 177 */       attrs.put("zimbraGalLdapSearchBase", domainBaseDn);
/* 178 */       attrs.put("zimbraGalLdapFilter", "zimbraAccounts");
/*     */       
/* 180 */       attrs.put("zimbraGalSyncLdapURL", "ldap://localhost:389");
/* 181 */       attrs.put("zimbraGalSyncLdapBindDn", LC.zimbra_ldap_userdn.value());
/* 182 */       attrs.put("zimbraGalSyncLdapBindPassword", LC.zimbra_ldap_password.value());
/* 183 */       attrs.put("zimbraGalSyncLdapSearchBase", domainBaseDn);
/* 184 */       attrs.put("zimbraGalSyncLdapFilter", "zimbraAccounts");
/* 185 */       domain = this.mProv.createDomain(domainName, attrs);
/*     */     }
/* 187 */     assertNotNull(domain);
/*     */     
/* 189 */     assertEquals(domain.getAttr("zimbraGalLdapURL"), "ldap://localhost:389");
/* 190 */     assertEquals(domain.getAttr("zimbraGalLdapBindDn"), LC.zimbra_ldap_userdn.value());
/* 191 */     assertEquals(domain.getAttr("zimbraGalLdapBindPassword"), LC.zimbra_ldap_password.value());
/* 192 */     assertEquals(domain.getAttr("zimbraGalLdapSearchBase"), domainBaseDn);
/* 193 */     assertEquals(domain.getAttr("zimbraGalLdapFilter"), "zimbraAccounts");
/*     */     
/* 195 */     assertEquals(domain.getAttr("zimbraGalSyncLdapURL"), "ldap://localhost:389");
/* 196 */     assertEquals(domain.getAttr("zimbraGalSyncLdapBindDn"), LC.zimbra_ldap_userdn.value());
/* 197 */     assertEquals(domain.getAttr("zimbraGalSyncLdapBindPassword"), LC.zimbra_ldap_password.value());
/* 198 */     assertEquals(domain.getAttr("zimbraGalSyncLdapSearchBase"), domainBaseDn);
/* 199 */     assertEquals(domain.getAttr("zimbraGalSyncLdapFilter"), "zimbraAccounts");
/*     */     
/* 201 */     setupTokenize(domainName, null, null);
/* 202 */     domain = this.mProv.get(Key.DomainBy.name, domainName);
/* 203 */     assertNotNull(domain);
/* 204 */     assertEquals(domain.getAttr("zimbraGalTokenizeAutoCompleteKey"), null);
/* 205 */     assertEquals(domain.getAttr("zimbraGalTokenizeSearchKey"), null);
/*     */   }
/*     */   
/*     */   private void createAccount(String userName, String firstName, String lastName) throws Exception {
/* 209 */     String acctName = userName + "@" + this.TOKENIZE_TEST_DOMAIN_NAME;
/*     */     
/* 211 */     Map<String, Object> attrs = new HashMap();
/* 212 */     if (firstName != null)
/* 213 */       attrs.put("givenName", firstName);
/* 214 */     if (lastName != null) {
/* 215 */       attrs.put("sn", lastName);
/*     */     }
/* 217 */     Account acct = this.mProv.get(Key.AccountBy.name, acctName);
/* 218 */     if (acct == null) {
/* 219 */       acct = this.mProv.createAccount(acctName, PASSWORD, attrs);
/*     */     }
/* 221 */     assertEquals(acct.getName(), acctName);
/* 222 */     if (firstName != null)
/* 223 */       assertEquals(acct.getAttr("givenName"), firstName);
/* 224 */     if (lastName != null)
/* 225 */       assertEquals(acct.getAttr("sn"), lastName);
/*     */   }
/*     */   
/*     */   public TestGal() throws Exception {
/* 229 */     this.mProv = Provisioning.getInstance();
/*     */     
/*     */ 
/* 232 */     setupDomain(this.DOMAIN_NAME, "dc=test-gal,dc=ldaptest");
/*     */     
/*     */ 
/* 235 */     if (this.SKIP_ACCT_CHECKING) {
/* 236 */       return;
/*     */     }
/* 238 */     for (int i = 0; i < NUM_ACCOUNTS; i++) {
/* 239 */       String acctName = acctName(i);
/* 240 */       Account acct = this.mProv.get(Key.AccountBy.name, acctName);
/* 241 */       if (acct == null)
/* 242 */         acct = this.mProv.createAccount(acctName, PASSWORD, new HashMap());
/* 243 */       assertNotNull(acct);
/*     */       
/* 245 */       if ((i + 1) % 100 == 0) {
/* 246 */         System.out.println("Created/checked " + (i + 1) + " accounts");
/*     */       }
/*     */     }
/*     */     
/* 250 */     setupDomain(this.TOKENIZE_TEST_DOMAIN_NAME, "dc=test-gal-tokenize,dc=ldaptest");
/* 251 */     createAccount("user1", "phoebe", "shao");
/* 252 */     createAccount("user2", "phoebe shao", null);
/* 253 */     createAccount("user3", null, "phoebe shao");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void setupAutoComplete(Provisioning.GalMode galMode, int pageSize)
/*     */     throws Exception
/*     */   {
/* 274 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 275 */     assertNotNull(domain);
/*     */     
/* 277 */     Map<String, Object> attrs = new HashMap();
/* 278 */     attrs.put("zimbraGalMode", galMode.toString());
/* 279 */     attrs.put("zimbraGalLdapPageSize", "" + pageSize);
/*     */     
/*     */ 
/*     */ 
/* 283 */     attrs.put("zimbraGalMaxResults", "" + more(NUM_ACCOUNTS));
/* 284 */     this.mProv.modifyAttrs(domain, attrs);
/*     */   }
/*     */   
/*     */   private void setupSearch(Provisioning.GalMode galMode, int pageSize, int domainLimit) throws Exception
/*     */   {
/* 289 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 290 */     assertNotNull(domain);
/*     */     
/* 292 */     Map<String, Object> attrs = new HashMap();
/* 293 */     attrs.put("zimbraGalMode", galMode.toString());
/* 294 */     attrs.put("zimbraGalLdapPageSize", "" + pageSize);
/* 295 */     attrs.put("zimbraGalMaxResults", "" + domainLimit);
/* 296 */     this.mProv.modifyAttrs(domain, attrs);
/*     */   }
/*     */   
/*     */   private void setupSync(Provisioning.GalMode galMode, int pageSize, int domainLimit) throws Exception
/*     */   {
/* 301 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 302 */     assertNotNull(domain);
/*     */     
/* 304 */     Map<String, Object> attrs = new HashMap();
/* 305 */     attrs.put("zimbraGalMode", galMode.toString());
/* 306 */     attrs.put("zimbraGalSyncLdapPageSize", "" + pageSize);
/* 307 */     attrs.put("zimbraGalMaxResults", "" + domainLimit);
/* 308 */     this.mProv.modifyAttrs(domain, attrs);
/*     */   }
/*     */   
/*     */   private static int less(int number) {
/* 312 */     assertFalse(UNLIMITED == number);
/* 313 */     return number / 2;
/*     */   }
/*     */   
/*     */   private static int more(int number) {
/* 317 */     assertFalse(UNLIMITED == number);
/* 318 */     return number * 2;
/*     */   }
/*     */   
/*     */   private void dumpResult(Provisioning.SearchGalResult galResult) throws Exception {
/* 322 */     for (GalContact contact : galResult.getMatches()) {
/* 323 */       System.out.println(contact.getId());
/*     */     }
/*     */   }
/*     */   
/*     */   private void autoCompleteGal(int numResultsExpected, int maxWanted) throws Exception {
/* 328 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 329 */     Provisioning.SearchGalResult galResult = this.mProv.autoCompleteGal(domain, QUERY, GalSearchType.account, maxWanted, null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 334 */     if (numResultsExpected != galResult.getNumMatches()) {
/* 335 */       dumpResult(galResult);
/*     */     }
/* 337 */     assertEquals(numResultsExpected, galResult.getNumMatches());
/* 338 */     boolean expectedHasMore = numResultsExpected < NUM_ACCOUNTS;
/* 339 */     assertEquals(expectedHasMore, galResult.getHadMore());
/*     */   }
/*     */   
/*     */   private void searchGal(int numResultsExpected) throws Exception {
/* 343 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 344 */     Provisioning.SearchGalResult galResult = this.mProv.searchGal(domain, QUERY, GalSearchType.all, 0, null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 349 */     assertEquals(numResultsExpected, galResult.getNumMatches());
/* 350 */     boolean expectedHasMore = numResultsExpected < NUM_ACCOUNTS;
/* 351 */     assertEquals(expectedHasMore, galResult.getHadMore());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String syncGal(int numResultsExpected, int numTotal, String token)
/*     */     throws Exception
/*     */   {
/* 361 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.DOMAIN_NAME);
/* 362 */     Provisioning.SearchGalResult galResult = this.mProv.syncGal(domain, token, null);
/* 363 */     assertEquals(numResultsExpected, galResult.getNumMatches());
/* 364 */     boolean expectedHasMore = numResultsExpected < numTotal;
/*     */     
/*     */ 
/*     */ 
/* 368 */     assertEquals(expectedHasMore, galResult.getHadMore());
/*     */     
/* 370 */     return galResult.getToken();
/*     */   }
/*     */   
/*     */   private void syncGal(int numResultsExpected) throws Exception
/*     */   {
/* 375 */     String token = syncGal(numResultsExpected, NUM_ACCOUNTS, null);
/* 376 */     assertNotNull(token);
/*     */     
/*     */ 
/* 379 */     Thread.sleep(1000L);
/*     */     
/*     */ 
/* 382 */     int numDelta = NUM_ACCOUNTS / 10;
/* 383 */     Map<String, Object> attrs = new HashMap();
/* 384 */     attrs.put("zimbraNotes", "blah");
/*     */     
/* 386 */     for (int i = 0; i < numDelta; i++) {
/* 387 */       String acctName = acctName(i);
/* 388 */       Account acct = this.mProv.get(Key.AccountBy.name, acctName);
/* 389 */       assertNotNull(acct);
/*     */       
/* 391 */       this.mProv.modifyAttrs(acct, attrs);
/*     */     }
/*     */     
/*     */ 
/* 395 */     syncGal(numDelta, numDelta, token);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void autoCompleteGal(Provisioning.GalMode galMode, int pageSize)
/*     */     throws Exception
/*     */   {
/* 409 */     if (this.DEBUG) {
/* 410 */       System.out.format("autoCompleteGal: %s, page size = %d\n", new Object[] { galMode, Integer.valueOf(pageSize) });
/*     */     }
/* 412 */     setupAutoComplete(galMode, pageSize);
/*     */     
/* 414 */     if (LDAP_SERVER_SIZE_LIMIT == UNLIMITED) {
/* 415 */       autoCompleteGal(NUM_ACCOUNTS, UNLIMITED);
/* 416 */       autoCompleteGal(NUM_ACCOUNTS, more(NUM_ACCOUNTS));
/* 417 */       autoCompleteGal(less(NUM_ACCOUNTS), less(NUM_ACCOUNTS));
/*     */     } else {
/* 419 */       autoCompleteGal(LDAP_SERVER_SIZE_LIMIT, UNLIMITED);
/* 420 */       autoCompleteGal(LDAP_SERVER_SIZE_LIMIT, more(LDAP_SERVER_SIZE_LIMIT));
/* 421 */       autoCompleteGal(less(LDAP_SERVER_SIZE_LIMIT), less(LDAP_SERVER_SIZE_LIMIT));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void searchGal(Provisioning.GalMode galMode, int pageSize)
/*     */     throws Exception
/*     */   {
/* 436 */     if (this.DEBUG) {
/* 437 */       System.out.format("searchGal: %s, page size = %d\n", new Object[] { galMode, Integer.valueOf(pageSize) });
/*     */     }
/* 439 */     if (LDAP_SERVER_SIZE_LIMIT == UNLIMITED) {
/* 440 */       setupSearch(galMode, pageSize, UNLIMITED);
/* 441 */       searchGal(NUM_ACCOUNTS);
/*     */       
/* 443 */       setupSearch(galMode, pageSize, more(NUM_ACCOUNTS));
/* 444 */       searchGal(NUM_ACCOUNTS);
/*     */       
/* 446 */       setupSearch(galMode, pageSize, less(NUM_ACCOUNTS));
/* 447 */       searchGal(less(NUM_ACCOUNTS));
/*     */     } else {
/* 449 */       setupSearch(galMode, pageSize, LDAP_SERVER_SIZE_LIMIT);
/* 450 */       searchGal(LDAP_SERVER_SIZE_LIMIT);
/*     */       
/* 452 */       setupSearch(galMode, pageSize, more(LDAP_SERVER_SIZE_LIMIT));
/* 453 */       searchGal(LDAP_SERVER_SIZE_LIMIT);
/*     */       
/* 455 */       setupSearch(galMode, pageSize, less(LDAP_SERVER_SIZE_LIMIT));
/* 456 */       searchGal(less(LDAP_SERVER_SIZE_LIMIT));
/*     */     }
/*     */   }
/*     */   
/*     */   private void syncGal(Provisioning.GalMode galMode, int pageSize) throws Exception {
/* 461 */     if (this.DEBUG) {
/* 462 */       System.out.format("syncGal: %s, page size = %d\n", new Object[] { galMode, Integer.valueOf(pageSize) });
/*     */     }
/* 464 */     if (LDAP_SERVER_SIZE_LIMIT == UNLIMITED) {
/* 465 */       setupSync(galMode, pageSize, UNLIMITED);
/* 466 */       syncGal(NUM_ACCOUNTS);
/*     */       
/* 468 */       setupSync(galMode, pageSize, more(NUM_ACCOUNTS));
/* 469 */       syncGal(NUM_ACCOUNTS);
/*     */       
/* 471 */       setupSync(galMode, pageSize, less(NUM_ACCOUNTS));
/* 472 */       syncGal(less(NUM_ACCOUNTS));
/*     */     } else {
/* 474 */       setupSync(galMode, pageSize, LDAP_SERVER_SIZE_LIMIT);
/* 475 */       syncGal(LDAP_SERVER_SIZE_LIMIT);
/*     */       
/* 477 */       setupSync(galMode, pageSize, more(LDAP_SERVER_SIZE_LIMIT));
/* 478 */       syncGal(LDAP_SERVER_SIZE_LIMIT);
/*     */       
/* 480 */       setupSync(galMode, pageSize, less(LDAP_SERVER_SIZE_LIMIT));
/* 481 */       syncGal(less(LDAP_SERVER_SIZE_LIMIT));
/*     */     }
/*     */   }
/*     */   
/*     */   public void autoCompleteGal(Provisioning.GalMode galMode) throws Exception {
/* 486 */     PageSizeEnum pse = new PageSizeEnum();
/* 487 */     while (pse.hasNext())
/* 488 */       autoCompleteGal(galMode, pse.next());
/*     */   }
/*     */   
/*     */   public void searchGal(Provisioning.GalMode galMode) throws Exception {
/* 492 */     PageSizeEnum pse = new PageSizeEnum();
/* 493 */     while (pse.hasNext())
/* 494 */       searchGal(galMode, pse.next());
/*     */   }
/*     */   
/*     */   public void syncGal(Provisioning.GalMode galMode) throws Exception {
/* 498 */     PageSizeEnum pse = new PageSizeEnum();
/* 499 */     while (pse.hasNext())
/* 500 */       syncGal(galMode, pse.next());
/*     */   }
/*     */   
/*     */   public void testAutoCompleteGal() throws Exception {
/* 504 */     autoCompleteGal(Provisioning.GalMode.zimbra);
/* 505 */     autoCompleteGal(Provisioning.GalMode.ldap);
/*     */   }
/*     */   
/*     */   public void testSearchGal() throws Exception {
/* 509 */     searchGal(Provisioning.GalMode.zimbra);
/* 510 */     searchGal(Provisioning.GalMode.ldap);
/*     */   }
/*     */   
/*     */   public void testSyncGal() throws Exception {
/* 514 */     syncGal(Provisioning.GalMode.zimbra);
/* 515 */     syncGal(Provisioning.GalMode.ldap);
/*     */   }
/*     */   
/*     */   private void setupTokenize(String domainName, Provisioning.GalMode galMode, String andOr) throws Exception {
/* 519 */     Domain domain = this.mProv.get(Key.DomainBy.name, domainName);
/* 520 */     assertNotNull(domain);
/*     */     
/* 522 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 524 */     if (galMode != null) {
/* 525 */       attrs.put("zimbraGalMode", galMode.toString());
/*     */     }
/* 527 */     if (andOr == null) {
/* 528 */       attrs.put("zimbraGalTokenizeAutoCompleteKey", "");
/* 529 */       attrs.put("zimbraGalTokenizeSearchKey", "");
/*     */     } else {
/* 531 */       attrs.put("zimbraGalTokenizeAutoCompleteKey", andOr);
/* 532 */       attrs.put("zimbraGalTokenizeSearchKey", andOr);
/*     */     }
/*     */     
/* 535 */     this.mProv.modifyAttrs(domain, attrs);
/*     */   }
/*     */   
/*     */   private void tokenizeTest(GalOp galOp, String tokenizeKey, String key, String[] expectedUsers) throws Exception {
/* 539 */     Domain domain = this.mProv.get(Key.DomainBy.name, this.TOKENIZE_TEST_DOMAIN_NAME);
/* 540 */     Provisioning.SearchGalResult galResult = null;
/*     */     
/* 542 */     if (galOp == GalOp.GOP_AUTOCOMPLETE) {
/* 543 */       galResult = this.mProv.autoCompleteGal(domain, key, GalSearchType.account, 10, null);
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/* 548 */     else if (galOp == GalOp.GOP_SEARCH) {
/* 549 */       galResult = this.mProv.searchGal(domain, key, GalSearchType.account, 0, null);
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/* 555 */       fail();
/*     */     }
/* 557 */     System.out.println("tokenizeTest: key=" + key);
/*     */     
/* 559 */     assertEquals(galResult.getTokenizeKey(), tokenizeKey);
/*     */     
/* 561 */     Set<String> results = new HashSet();
/* 562 */     for (GalContact gc : galResult.getMatches()) {
/* 563 */       String r = (String)gc.getAttrs().get(ContactConstants.A_email);
/* 564 */       System.out.println("    " + r);
/* 565 */       results.add(r);
/*     */     }
/*     */     
/* 568 */     for (String mail : expectedUsers) {
/* 569 */       assertTrue(results.contains(mail + "@" + this.TOKENIZE_TEST_DOMAIN_NAME));
/*     */     }
/* 571 */     List<String> expectedUsersList = Arrays.asList(expectedUsers);
/* 572 */     for (String mail : results) {
/* 573 */       assertTrue(expectedUsersList.contains(mail.split("@")[0]));
/*     */     }
/* 575 */     assertEquals(expectedUsers.length, galResult.getNumMatches());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void autoCompleteWithTokenizeAND(Provisioning.GalMode galMode)
/*     */     throws Exception
/*     */   {
/* 587 */     setupTokenize(this.TOKENIZE_TEST_DOMAIN_NAME, galMode, GalConstants.TOKENIZE_KEY_AND);
/*     */     
/* 589 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_AND, "phoebe", new String[] { "user1", "user2", "user3" });
/* 590 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_AND, "shao", new String[] { "user1" });
/* 591 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_AND, "phoebe shao", new String[] { "user1" });
/* 592 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_AND, "phoebe blah", new String[0]);
/* 593 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_AND, "blah shao", new String[0]);
/*     */   }
/*     */   
/*     */   public void autoCompleteWithTokenizeOR(Provisioning.GalMode galMode) throws Exception {
/* 597 */     setupTokenize(this.TOKENIZE_TEST_DOMAIN_NAME, galMode, GalConstants.TOKENIZE_KEY_OR);
/*     */     
/* 599 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_OR, "phoebe", new String[] { "user1", "user2", "user3" });
/* 600 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_OR, "shao", new String[] { "user1" });
/* 601 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_OR, "phoebe shao", new String[] { "user1", "user2", "user3" });
/* 602 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_OR, "phoebe blah", new String[] { "user1", "user2", "user3" });
/* 603 */     tokenizeTest(GalOp.GOP_AUTOCOMPLETE, GalConstants.TOKENIZE_KEY_OR, "blah shao", new String[] { "user1" });
/*     */   }
/*     */   
/*     */   public void searchWithTokenizeAND(Provisioning.GalMode galMode) throws Exception {
/* 607 */     setupTokenize(this.TOKENIZE_TEST_DOMAIN_NAME, galMode, GalConstants.TOKENIZE_KEY_AND);
/*     */     
/* 609 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_AND, "phoebe", new String[] { "user1", "user2", "user3" });
/* 610 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_AND, "shao", new String[] { "user1", "user2", "user3" });
/* 611 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_AND, "phoebe shao", new String[] { "user1", "user2", "user3" });
/* 612 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_AND, "phoebe blah", new String[0]);
/* 613 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_AND, "blah shao", new String[0]);
/*     */   }
/*     */   
/*     */   public void searchWithTokenizeOR(Provisioning.GalMode galMode) throws Exception {
/* 617 */     setupTokenize(this.TOKENIZE_TEST_DOMAIN_NAME, galMode, GalConstants.TOKENIZE_KEY_OR);
/*     */     
/* 619 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_OR, "phoebe", new String[] { "user1", "user2", "user3" });
/* 620 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_OR, "shao", new String[] { "user1", "user2", "user3" });
/* 621 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_OR, "phoebe shao", new String[] { "user1", "user2", "user3" });
/* 622 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_OR, "phoebe blah", new String[] { "user1", "user2", "user3" });
/* 623 */     tokenizeTest(GalOp.GOP_SEARCH, GalConstants.TOKENIZE_KEY_OR, "blah shao", new String[] { "user1", "user2", "user3" });
/*     */   }
/*     */   
/*     */   public void testTokenizeKey() throws Exception {
/* 627 */     autoCompleteWithTokenizeAND(Provisioning.GalMode.zimbra);
/* 628 */     autoCompleteWithTokenizeAND(Provisioning.GalMode.ldap);
/* 629 */     autoCompleteWithTokenizeOR(Provisioning.GalMode.zimbra);
/* 630 */     autoCompleteWithTokenizeOR(Provisioning.GalMode.ldap);
/*     */     
/* 632 */     searchWithTokenizeAND(Provisioning.GalMode.zimbra);
/* 633 */     searchWithTokenizeAND(Provisioning.GalMode.ldap);
/* 634 */     searchWithTokenizeOR(Provisioning.GalMode.zimbra);
/* 635 */     searchWithTokenizeOR(Provisioning.GalMode.ldap);
/*     */   }
/*     */   
/*     */   public void disable_testPageSizeEnum() throws Exception
/*     */   {
/* 640 */     PageSizeEnum pse = null;
/* 641 */     LDAP_SERVER_SIZE_LIMIT = LIMITED;
/* 642 */     System.out.println("limited");
/* 643 */     pse = new PageSizeEnum();
/* 644 */     while (pse.hasNext()) {
/* 645 */       System.out.println(pse.next());
/*     */     }
/*     */     
/* 648 */     LDAP_SERVER_SIZE_LIMIT = UNLIMITED;
/* 649 */     System.out.println("unlimited");
/* 650 */     pse = new PageSizeEnum();
/* 651 */     while (pse.hasNext()) {
/* 652 */       System.out.println(pse.next());
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/*     */     try {
/* 658 */       TestUtil.runTest(TestGal.class);
/*     */     } catch (Exception e) {
/* 660 */       System.out.println(e.getMessage());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestGal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */